const app = getApp();
const regeneratorRuntime = global.regeneratorRuntime = require("../libs/regenerator");
const co = require('../libs/co');
const time = require('../libs/util');


Page({
  data:{
    hidden:false,
    order:{},
    paymentText:"",
    createDate: "",
    orderNum:""
  },

  onReady() {

  },
  onLoad: function(options) {
      app.launchOptions = options;
      /* !Do not Remove the follow line, used for build tool.*/
      let orderNum = options.orderNum;
      this.setData({
        orderNum:orderNum
      })
  },
  loadOrder: function (orderNum) {
    const self = this;
    co(function *() {
        let orderService = require("../services/mall/orderService");
        let order = yield orderService.info(orderNum);
        let pText = order.paymentType == 0 ? "在线支付" : "货到付款";
        let dateStr = time.formatTimeUtc(new Date(order.addTime * 1000));

        self.setData({
            hidden:true,
            order: order,
            paymentText: pText,
            createDate: dateStr
        });
    });
  },

  onShow:function(){
    this.loadOrder(this.data.orderNum);
  },
  onTouchedOrder:function(e) {
    let orderNum = e.currentTarget.dataset.no;
    let status = e.currentTarget.dataset.t;

    if (status.toUpperCase() == "CANCEL_ORDER" || status.toUpperCase() == "DEL_ORDER" || status.toUpperCase() == "COMFIRM_RECEIVE") {
      this.showModel(orderNum,status);
    } else if(status.toUpperCase() == "APPLY_REFUND") {
      wx.navigateTo({
        url: "/pages/page13?orderNum=" + orderNum,
      })
    }
  },
  showModel: function (orderNum,status){
    let msgText = "是否确认取消此订单?";
    if (status.toUpperCase() == "COMFIRM_RECEIVE") {
       msgText = "是否确认收货?";
    } else if (status.toUpperCase() == "DEL_ORDER") {
       msgText = "是否确认删除此订单?";
    }
    const self = this;

    wx.showModal({
          title: '提示',
          content: msgText,
          confirmText: "确定",
          cancelText: "取消",
          success: function (res) {
              if (res.confirm) {

                  self.doOrder(orderNum,status);
              }else{
              }
          }
      });
  },
  doOrder: function (orderNum,status) {
    this.setData({
      hidden:false
    });
    const self = this;
    co(function *() {
        let orderService = require("../services/mall/orderService");
        let ret = yield orderService.doOrder(orderNum,status);
        console.log(ret);
        self.setData({
          hidden:true
        });
        if (ret) {
          wx.navigateBack({});
        }
    });
  }
})
